//
//  SelectView.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/24.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "SelectView.h"
#import "CJSLunboView.h"
#import "MainTopCell.h"
#import "DownTableViewCell.h"
#import "Resources.h"
#import "TypeFour.h"
#import "TypeTen.h"
#import "Lunbo.h"
#import "CJSStoryViewController.h"
#import "CJSTravelNotesViewController.h"
#import "CJSDetailViewController.h"
#import "CJSHeadView.h"
#import "MJRefresh.h"

@interface SelectView () <UITableViewDataSource, UITableViewDelegate, MainTopCellDelegate, CJSLunboViewDelegate>

@property (nonatomic, retain) UITableView *mainTableView;

@property (nonatomic, retain) CJSLunboView *lunboView;

@end


@implementation SelectView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.mainTableView.backgroundColor = self.backgroundColor;
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.separatorStyle = NO;
    [self addSubview:self.mainTableView];
    self.mainTableView.tableHeaderView = self.lunboView;
    
    [self.mainTableView registerClass:[MainTopCell class] forCellReuseIdentifier:@"identifyTop"];
    [self.mainTableView registerClass:[DownTableViewCell class] forCellReuseIdentifier:@"identifyDown"];
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifyLabel"];
    
    self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.delegate getDataWithNext:NO];
        
    }];
    
    self.mainTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self.delegate getDataWithNext:YES];
    }];
        

    [self.mainTableView.mj_header beginRefreshing];
}

#pragma mark - tableview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return SCREEN_HEIGHT * 520 / 667;
    }
    else {
        
        return 210 * ADAPT_WIDTH;
    }
    
}

#pragma mark section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.fourArray.count == 0) {
        return 0;
    }
    return 2;
}
#pragma mark row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    return self.fourArray.count;
}
#pragma mark section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return ADAPT_HEIGHT * 70;
}
#pragma mark sectionView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    CJSHeadView *headView = [[CJSHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADAPT_HEIGHT * 70)];
    [headView showSectionTitle:section];
    return headView;
}

#pragma mark cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSTravelNotesViewController *travelNVC = [[CJSTravelNotesViewController alloc] init];
    travelNVC.spot_id = [[self.fourArray objectAtIndex:indexPath.row] tId];
    [self.delegate pushDetailViewController:travelNVC];
    
}

#pragma mark cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        MainTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifyTop"];
        cell.delegate = self;
        CGRect frame = cell.contentView.frame;
        frame.size.height = SCREEN_HEIGHT * 520 / 667;
        frame.size.width = self.frame.size.width;
        cell.contentView.frame = frame;
        cell.contentView.backgroundColor = self.backgroundColor;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (self.tenArray) {
            cell.tenArray = self.tenArray;
        }
        return cell;
    }
    else {
        
        DownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifyDown"];
        CGRect frame = cell.contentView.frame;
        frame.size.height = 270;
        frame.size.width = self.frame.size.width;
        cell.contentView.frame = frame;
        cell.contentView.backgroundColor = self.backgroundColor;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.frame = frame;
        if (self.fourArray) {
            cell.typeFour = [self.fourArray objectAtIndex:indexPath.row];

        }
        return cell;
    }
}

#pragma mark - 轮播图
- (void)setLunboArray:(NSMutableArray *)lunboArray {
    
    _lunboArray = lunboArray;

    for (UIView *view in [self.lunboView subviews]) {
        
        [view removeFromSuperview];
    }
    if (lunboArray.count != 0) {
        [self.lunboView createSubViews:lunboArray];
    }
    
    //    [self.mainTableView reloadData];
    //    [self.mainTableView.header endRefreshing];
}

#pragma mark - 结束刷新
- (void)endRefresh {
    
    [self.mainTableView reloadData];
    [self.mainTableView.mj_header endRefreshing];
}

- (void)endFooterRefresh {
    
    [self.mainTableView.mj_footer endRefreshing];
    [self.mainTableView reloadData];
}

#pragma mark -
- (void)barAction:(UIBarButtonItem *)bar {
    
    
}
#pragma 轮播图
- (CJSLunboView *)lunboView {
    
    if (!_lunboView) {
        
        self.lunboView = [[CJSLunboView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200 * ADAPT_HEIGHT)];
        self.lunboView.delegate = self;
    }
    
    return _lunboView;
}

- (void)getTapNumber:(NSInteger)number {
    
    CJSDetailViewController *detailVC = [[CJSDetailViewController alloc] init];
    
    detailVC.htmlStr = [[self.lunboArray objectAtIndex:number] html_url];
    [self.delegate pushDetailViewController:detailVC];
}

#pragma mark - 故事代理方法
- (void)pushViewController:(NSString *)spot_id {
    
    CJSStoryViewController *storyVC = [[CJSStoryViewController alloc] init];
    storyVC.spot_id = spot_id;
    [self.delegate pushDetailViewController:storyVC];

}

#pragma mark section不悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 70 * ADAPT_WIDTH;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


@end
